package com.lzkj.baotouhousingfund.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import com.lzkj.baotouhousingfund.app.App;
import com.lzkj.baotouhousingfund.di.component.DaggerFragmentComponent;
import com.lzkj.baotouhousingfund.di.component.FragmentComponent;
import com.lzkj.baotouhousingfund.di.module.FragmentModule;
import com.trello.rxlifecycle2.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by codeest on 2016/8/2.
 * MVP Fragment基类
 */
public abstract class BaseFragment<T extends BasePresenter> extends RxFragment implements BaseView {


    @Inject
    protected T        mPresenter;
    protected View     mView;
    protected Activity mActivity;
    protected Context  mContext;
    private   Unbinder mUnBinder;
    protected boolean isInited = false;
    private   InputMethodManager        mInputMethodManager;


    @Override
    public void onAttach(Context context) {
        mActivity = (Activity) context;
        mContext = context;
        super.onAttach(context);
    }

    protected FragmentComponent getFragmentComponent() {
        return DaggerFragmentComponent.builder()
                .appComponent(App.getAppComponent())
                .fragmentModule(getFragmentModule())
                .build();
    }

    protected FragmentModule getFragmentModule() {
        return new FragmentModule(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);
        initInject();
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mPresenter.attachView(this);
        mUnBinder = ButterKnife.bind(this, view);
        if (savedInstanceState == null) {
            if (!isHidden()) {
                isInited = true;
                initEventAndData();
            }
        } else {
//            if (!isSupportHidden()) {
            isInited = true;
            initEventAndData();
//            }
        }
        //如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            //注册到事件主线
            EventBus.getDefault().register(this);
        }
    }





    public boolean useEventBus() {
        return false;
    }


    public void hideSoftInput(View view) {
        if (mInputMethodManager == null) {
            initInputMetodManager();
        }
        mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    private void initInputMetodManager() {
        mInputMethodManager = (InputMethodManager) getActivity().getSystemService(Context
                .INPUT_METHOD_SERVICE);
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!isInited && !hidden) {
            isInited = true;
            initEventAndData();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
        //如果要使用eventbus请将此方法返回true
        if (useEventBus()) {
            //注册到事件主线
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    protected abstract void initInject();

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();
}